
package com.qiwx.pigeon.listviewitems;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.data.vo.SummaryDaliy;

import java.util.ArrayList;
import java.util.List;

public class LineChartItem extends ChartItem {

    private final Typeface mTf;
    List<SummaryDaliy> data;
    List<String> mXaxisList = new ArrayList<>();


    public LineChartItem(ChartData<?> cd, Context c, List<SummaryDaliy> data) {
        super(cd);

        mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf");
        this.data = data;
       // initXDate();
    }
    private void initXDate(){
        for (int i = 0; i < data.size(); i++) {
            String date = data.get(i).getLay_egg_date();
            date = DateTimeUtils.formatDate(date, "MM/dd");
            mXaxisList.add(date);
        }
    }

    @Override
    public int getItemType() {
        return TYPE_LINECHART;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, Context c) {

        ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();

            convertView = LayoutInflater.from(c).inflate(
                    R.layout.list_item_linechart, null);
            holder.chart = convertView.findViewById(R.id.chart);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // apply styling
        // holder.chart.setValueTypeface(mTf);
        holder.chart.getDescription().setEnabled(false);
        holder.chart.setDrawGridBackground(false);
//
//        XAxis xAxis = holder.chart.getXAxis();
//        xAxis.setPosition(XAxisPosition.BOTTOM);
//        xAxis.setTypeface(mTf);
//        xAxis.setDrawGridLines(false);
//        xAxis.setDrawAxisLine(false);
        //xAxis.setValueFormatter(formatter);


        YAxis leftAxis = holder.chart.getAxisLeft();
        leftAxis.setTypeface(mTf);
        leftAxis.setLabelCount(5, false);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        YAxis rightAxis = holder.chart.getAxisRight();
        rightAxis.setTypeface(mTf);
        rightAxis.setLabelCount(5, false);
        rightAxis.setDrawGridLines(false);
        rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)


        // set data
        holder.chart.setData((LineData) mChartData);

        // do not forget to refresh the chart
        // holder.chart.invalidate();
        holder.chart.animateX(750);

        return convertView;
    }

    private static class ViewHolder {
        LineChart chart;
    }
    ValueFormatter formatter=new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            List<String> xAxisList = getXaxisList();
            int v = (int) value;
            if (v <= xAxisList.size() && v >= 0) {
                String st = xAxisList.get(v);
                return st;
            } else {
                return null;
            }
        }
    };
    /**
     * 为设置x轴的日期，生成对应格式的数据
     *
     * @param
     * @return mXaxisList
     */
    private List<String> getXaxisList() {
        return mXaxisList;
    }

}
